package com.quanquan.store.service;

import DTO.Commercial.CommercialInfoDTO;
import DTO.PageDTO;
import com.quanquan.store.dto.CommercialDTO;
import com.quanquan.store.entity.Commercial;
import com.quanquan.store.entity.Store;
import com.quanquan.store.entity.StoreCommercial;
import com.quanquan.store.repository.CommercialRepository;
import com.quanquan.store.repository.StoreCommercialRepository;
import com.quanquan.store.repository.StoreRepository;
import com.quanquan.store.request.CommercialRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import request.PageRequestBody;
import utils.DateUtils;
import utils.UUIDGenerator;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName:CommercialService
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/3/12 10:41
 * @Version 1.0
 */
@Service
public class CommercialService {

    private final CommercialRepository commercialRepository;
    private final StoreRepository storeRepository;
    private final StoreCommercialRepository storeCommercialRepository;
    private final RedisTemplate redisTemplate;
    private static final String COMMERCIAL = "commercial_";


    @Autowired
    public CommercialService(CommercialRepository commercialRepository, StoreRepository storeRepository, StoreCommercialRepository storeCommercialRepository, RedisTemplate redisTemplate) {
        this.commercialRepository = commercialRepository;
        this.storeRepository = storeRepository;
        this.storeCommercialRepository = storeCommercialRepository;
        this.redisTemplate = redisTemplate;
    }

    /**
     * 添加业态
     *
     * @param request
     * @return
     */
    public CommercialDTO saveCommercial(CommercialRequest request) {
        Date date = new Date();
        Commercial commercial = new Commercial();
        commercial.setId(UUIDGenerator.getUUID());
        commercial.setCreateTime(DateUtils.dateToString(date, DateUtils.TIMESTAMP));
        commercial.setUpdateTime(DateUtils.dateToString(date, DateUtils.TIMESTAMP));
        commercial.setStatusShow(request.getStatusShow());
        commercial.setStatusRemove(0);
        commercial.setName(request.getName());
        commercial.setSortOrder(request.getSortOrder());
        commercial.setPercent(request.getPercent());
        commercialRepository.saveAndFlush(commercial);
        CommercialDTO commercialDTO = new CommercialDTO();
        BeanUtils.copyProperties(commercial, commercialDTO);
        return commercialDTO;
    }

    /**
     * 查找有效的业态列表
     *
     * @param page
     * @param size
     * @return
     */
    public List<CommercialDTO> list(Integer page, Integer size) {
        List<CommercialDTO> commercialDTOList = new LinkedList<>();
        List<Commercial> commercialList = null;
        Sort sort = new Sort(Sort.Direction.ASC, "sortOrder");
        Pageable pageable = PageRequest.of(page, size, sort);
        Commercial commercial = new Commercial();
        commercial.setStatusShow(0);
        commercial.setStatusRemove(0);
        Example<Commercial> example = Example.of(commercial);
        Page<Commercial> findAll = commercialRepository.findAll(example, pageable);
        commercialList = findAll.getContent();
        for (Commercial localCommercial : commercialList) {
            CommercialDTO commercialDTO = new CommercialDTO();
            BeanUtils.copyProperties(localCommercial, commercialDTO);
            commercialDTOList.add(commercialDTO);
        }
        return commercialDTOList;
    }

    /**
     * 删除业态（软删除）
     *
     * @param id
     * @return
     */
    public Integer removeStore(String id) {
        Date date = new Date();
        Commercial commercial = commercialRepository.findByCommercialId(id);
        commercial.setStatusRemove(1);
        commercial.setUpdateTime(DateUtils.dateToString(date, DateUtils.TIMESTAMP));
        commercialRepository.saveAndFlush(commercial);
        return commercial.getStatusRemove();
    }

    /**
     * 修改业态信息
     *
     * @param request
     * @return
     */
    public boolean updateCommercial(CommercialRequest request) {
        Date date = new Date();
        Commercial commercial = commercialRepository.findByCommercialId(request.getId());
        if (commercial == null) {
            return false;
        }
        List<Store> storeList = storeRepository.findByCommercialId(commercial.getId());
        for (Store store : storeList) {
            store.setStoreCommercial(request.getName());
            storeRepository.saveAndFlush(store);
        }
        commercial.setUpdateTime(DateUtils.dateToString(date, DateUtils.TIMESTAMP));
        commercial.setStatusShow(request.getStatusShow());
        commercial.setStatusRemove(0);
        commercial.setName(request.getName());
        commercial.setSortOrder(request.getSortOrder());
        commercial.setPercent(request.getPercent());
        commercialRepository.saveAndFlush(commercial);
        String id = commercial.getId();
        Integer percent = commercial.getPercent();
        redisTemplate.opsForValue().set(COMMERCIAL+id,percent);

        return true;
    }

    public PageDTO<Commercial> allCommercialForManagement(PageRequestBody requestBody) {
        Integer size = requestBody.getSize();
        Integer page = requestBody.getPage();
        Sort sort = new Sort(Sort.Direction.ASC, "sortOrder");
        Pageable pageable = PageRequest.of(page, size, sort);
        Commercial commercial = new Commercial();
        commercial.setStatusRemove(0);
        Example<Commercial> example = Example.of(commercial);
        Page<Commercial> findAll = commercialRepository.findAll(example, pageable);
        PageDTO<Commercial> pageDTO = new PageDTO<>();
        pageDTO.setTotal(findAll.getTotalElements());
        pageDTO.setList(findAll.getContent());
        pageDTO.setTotalPage(findAll.getTotalPages());
        pageDTO.setPage(page);
        pageDTO.setPageSize(size);
        return pageDTO;
    }


    /**
     * 根据业态id查看业态信息
     * @param request
     * @return
     */
    public CommercialDTO query(CommercialRequest request) {
        Commercial commercial = commercialRepository.findByCommercialId(request.getId());

        CommercialDTO commercialDTO = new CommercialDTO();
        //通过映射关系来赋值 //TODO 断点看一下
        BeanUtils.copyProperties(commercial, commercialDTO);
        return commercialDTO;
    }


    /**
     * 查找有效的业态列表
     *
     * @return
     */
    public List<CommercialDTO> queryListByStoreId(String storeId) {
        List<CommercialDTO> commercialDTOList = new LinkedList<>();
        List<StoreCommercial> storeCommercialList = storeCommercialRepository.findByStoreIdAndStatus(storeId);
        for (StoreCommercial storeCommercial : storeCommercialList){
            Commercial commercial = commercialRepository.findByIdAndAndStatusRemoveAndAndStatusShow(storeCommercial.getCommercialId());
            CommercialDTO commercialDTO = new CommercialDTO();
            commercialDTO.setId(commercial.getId());
            commercialDTO.setCreateTime(commercial.getCreateTime());
            commercialDTO.setName(commercial.getName());
            commercialDTO.setUpdateTime(commercial.getUpdateTime());
            commercialDTOList.add(commercialDTO);
        }
        return commercialDTOList;
    }

    public List<CommercialDTO> list() {
        List<CommercialDTO> commercialDTOList = new LinkedList<>();
        List<Commercial> commercialList = commercialRepository.list();
        for (Commercial localCommercial : commercialList) {
            CommercialDTO commercialDTO = new CommercialDTO();
            BeanUtils.copyProperties(localCommercial, commercialDTO);
            commercialDTOList.add(commercialDTO);
        }
        return commercialDTOList;
    }

    /**
     * 根据业态id查询业态的信息
     * @param commercialId
     */
    public CommercialInfoDTO findCommercialInfo(String commercialId) {
        CommercialInfoDTO commercialInfo = new CommercialInfoDTO();
        Commercial commercial = commercialRepository.findByIdAndAndStatusRemoveAndAndStatusShow(commercialId);
        if(commercial!=null){
            String id = commercial.getId();
            Integer percent = commercial.getPercent();
            redisTemplate.opsForValue().set(COMMERCIAL+id,percent);
            commercialInfo.setPercent(commercial.getPercent());
        }
        return commercialInfo;
    }
}
